export default {
  /**
   * 将数字转化成对应的科技文字描述
   * @param val
   */
  data() {
    return {
      domain: 'https://img.fireleaves.cn/',
      m_versions: [{
        label: 'DS',
        value: 'DS'
      },
      {
        label: 'DST',
        value: 'DST'
      },
      {
        label: 'ROG',
        value: 'ROG'
      },
      {
        label: 'SW',
        value: 'SW'
      },
      {
        label: 'HAMLET',
        value: 'HAMLET'
      }
      ]
    }
  },
  methods: {
    /**
     * @description 将后台的数据转化成前端需要的数据结构
     */
    showDataTransform(obj, arr) {
      let temp = {}
      arr.forEach(value => {
        temp[value.key] = obj[value.key].map(val => {
          if (value.needNum) {
            return {
              obj: val.item,
              num: val.num,
              selectOptions: [{
                list: [val.item],
                type: val.kind
              }]
            }
          } else if (value.hasList) {
            // 额外链接中处理嵌套
            val.list = val.list.map(listVal => {
              return {
                obj: listVal.item,
                num: listVal.num || '',
                selectOptions: [{
                  list: [listVal.item],
                  type: listVal.kind
                }]
              }
            })
            return val
          } else {
            return {
              obj: val.item,
              selectOptions: [{
                list: [val.item],
                type: val.kind
              }]
            }
          }
        })
      })
      return {
        ...obj,
        ...temp
      }
    },
    /**
     * @description 转化成二维数组，以支持或关系
     * @param {@} obj 
     * @param {*} arr 
     */
    showDataTransform2(arr) {
      let temp = arr.map(val => {
        return val.single.map(value => {
          return {
            obj: value.item,
            num: value.num,
            selectOptions: [{
              list: [value.item],
              type: value.kind
            }]
          }
        })
      })
      return temp
    },
    /**
     * @description 将前端的数据结构转化成后台需要的
     * @param {Array} arr
     */
    transformSumbitData(arr) {
      let result = []
      arr.forEach(value => {
        let temp
        if (!value.list.length || !value.list[0].obj) {
          temp = []
        } else {
          temp = value.list.map(val => {
            let activeIndex; // 获取下拉框中被点击的项的索引
            for (let i in val.selectOptions) {
              for (let j in val.selectOptions[i].list) {
                if (val.selectOptions[i].list[j]._id === val.obj._id) {
                  activeIndex = i
                }
              }
            }
            if (value.needNum) {
              return {
                kind: val.selectOptions[activeIndex].type,
                item: val.obj._id,
                num: val.num
              }
            } else {
              return {
                kind: val.selectOptions[activeIndex].type,
                item: val.obj._id
              }
            }
          })
        }
        result = [...result, temp]
      })
      return result
    },
    /**
     * @description 二维数组数据提交时的数据转化
     * @param {} arr 
     */
    transformSumbitData2(arr) {
      let result = arr.map(val => {
        let temp = val.map(value => {
          return {
            kind: 'foods', // FIXME 如果只是针对food中的need字段，则可以写死为foods
            item: value.obj._id,
            num: value.num
          }
        })
        return {
          "single": temp
        }
      })
      return result
    },
    transformSumbitDataList(arr) {
      console.log(arr)
      let result = arr.map(val => {
        let temp = val.list.map(value => {
          let kind = ''
          for (let i in value.selectOptions) {
            for (let j in value.selectOptions[i].list) {
              if (value.selectOptions[i].list[j]._id === value.obj._id) {
                kind = value.selectOptions[i].type
              }
            }
          }
          return {
            kind,
            item: value.obj._id,
            num: value.num
          }
        })
        return {
          ...val,
          list: temp
        }
      })
      return result
    }
  }
}